package org.springmvc.service;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springmvc.entity.User;
import org.springmvc.entity.enumeration.Gender;
import org.springmvc.mapper.UserMapper;
import org.springmvc.mybatis.pagination.Pageable;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tangjialin on 2018-08-13.
 */
@CacheConfig(cacheNames = "user")
@Service
public class UserService extends BaseService<User> {
    @Resource
    private UserMapper userMapper;

    @Cacheable(key = "#id")
    public User findCache(Long id) {
        return find(id);
    }

    public List<User> listByGender(Pageable pageable, Gender gender) {
        return userMapper.findListByGender(pageable, gender);
    }

    public List<User> list(Pageable pageable) {
        return userMapper.findList(pageable);
    }

    @CachePut(key = "#user.id", unless = "#result.id == null")
    @Override
    public User insertOrUpdate(User user) {
        return super.insertOrUpdate(user);
    }

    @CacheEvict(allEntries = true)
    public void refresh() {
    }
}
